#include "base_autoresize_table.h"

namespace
{

QString create_random_length_string(int max_length)
{
    const int length = qrand() % max_length;
    return QString(length, 'X');
}

}

base_autoresize_table::base_autoresize_table(QWidget *parent)
    : QTableView(parent)
    , model_(new QStandardItemModel(this))
{
    setModel(model_);
    setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);

//    setSizePolicy(QSizePolicy::Minimum         , QSizePolicy::Minimum         );
//    setSizePolicy(QSizePolicy::Fixed           , QSizePolicy::Fixed           );
//    setSizePolicy(QSizePolicy::Maximum         , QSizePolicy::Maximum         );
//    setSizePolicy(QSizePolicy::Preferred       , QSizePolicy::Preferred       );
//    setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
//    setSizePolicy(QSizePolicy::Expanding       , QSizePolicy::Expanding       );
//    setSizePolicy(QSizePolicy::Ignored         , QSizePolicy::Ignored         );
}

void base_autoresize_table::create_table(int row, int col)
{
    clear_table();
    if (model_->rowCount() > 0 || model_->columnCount() > 0)
        return;


    for (int i = 0; i < col; ++i)
    {
        QStandardItem *header_item = new QStandardItem("header" + QString::number(qrand()%1000000));
//        QStandardItem *header_item = new QStandardItem("h");
        model_->setHorizontalHeaderItem(i, header_item) ;
    }

    for (int i = 0; i < row; ++i)
    {
        QList<QStandardItem*> items;
        for (int j = 0; j < col; ++j)
        {
            items << new QStandardItem(create_random_length_string(j % 2 == 0 ? 5 : 10));
        }

        model_->appendRow(items);
    }
}

void base_autoresize_table::clear_table()
{
    model_->clear();
}

void base_autoresize_table::add_row()
{
    QList<QStandardItem*> items;
    for (int i = 0; i < model_->columnCount(); ++i)
        items << new QStandardItem("Ole-ole-ole");

    model_->appendRow(items);

    auto_resize();
}

void base_autoresize_table::del_row()
{
    model_->removeRow(0);

    auto_resize();
}

void base_autoresize_table::add_col()
{
    QList<QStandardItem*> items;
    for (int i = 0; i < model_->rowCount(); ++i)
        items << new QStandardItem("Ole-ole-ole");

    model_->appendColumn(items);

    QStandardItem *header_item = new QStandardItem("header" + QString::number(qrand()%1000000));
    model_->setHorizontalHeaderItem(model_->columnCount() - 1, header_item) ;

    auto_resize();
}

void base_autoresize_table::del_col()
{
    model_->removeColumn(0);
    auto_resize();
}

QSize base_autoresize_table::sizeHint() const
{
    return minimumSize();
}

void base_autoresize_table::resizeEvent(QResizeEvent *e)
{
//    if(horizontalScrollBar()->minimum() < horizontalScrollBar()->maximum())
//        horizontalScrollBar()->show();
//    else
//        horizontalScrollBar()->hide();

    QTableView::resizeEvent(e);
}

void base_autoresize_table::adjust_parent_size()
{
    auto parent = parentWidget();
    while (parent)
    {
        parent->adjustSize();
        parent = parent->parentWidget();
    }
}

int base_autoresize_table::sizeHintForColumn(int column) const
{
    if (!model())
        return -1;

    ensurePolished();

    int hint = 0;
    const QStyleOptionViewItemV4 option = static_cast<QStyleOptionViewItemV4>(viewOptions());
    for (int i = 0; i < model()->rowCount(); ++i)
    {
        const auto index = model()->index(i, column);
        hint = qMax(hint, itemDelegate(index)->sizeHint(option, index).width());
    }

    return showGrid() ? hint + 1 : hint;
}

int base_autoresize_table::sizeHintForRow(int /*row*/) const
{
    if (!model())
        return -1;

    return 20;
}


